package com.hdoit.kylin.web.baseInfo.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.baseInfo.model.Academy;
import com.hdoit.kylin.logical.baseInfo.service.IAcademyService;
import com.hdoit.kylin.logical.baseInfo.service.ISchoolService;
import com.hdoit.kylin.logical.viewmodel.baseInfo.AcademySearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.ListItem;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/baseInfo/academy/")
public class AcademyController extends BaseController {
	@Override
	public String getRoutePath(){
		return "areas/admin/baseInfo/academy/";
	}
	
	private IAcademyService academyService;
	
	public IAcademyService getAcademyService() {
		return academyService;
	}
	
	@Resource
	public void setAcademyService(IAcademyService academyService) {
		this.academyService = academyService;
	}
	
	private ISchoolService schoolServer;
	
	public ISchoolService getSchoolService() {
		return schoolServer;
	}
	
	@Resource
	public void setSchoolService(ISchoolService schoolService) {
		this.schoolServer = schoolService;
	}
	
	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "academyDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	private final String getAcademyBySchoolIdViewName = "getAcademyBySchoolId";
	
	@RequestMapping(value=indexViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel, AcademySearch search, boolean ajaxPost) {
		pagerModel.setTotalAmount(academyService.queryCount(search));
		List<Academy> academys = academyService.queryList(search, pagerModel);
		
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("academys", academys);
		model.addAttribute("academySearch", search);
		
		if(ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String academyDetailed(Model model, String key) {
		Academy academy = new Academy();
		
		if(!StringUtils.isEmpty(key)) {
			
			academy = academyService.queryModel(key);
		}
		model.addAttribute("academy", academy);
		return view(detailedViewName);
	}
	
	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, Academy academy) {
		if (StringUtils.isEmpty(academy.getAcademyId())) {
			academy.setAcademyId(ChineseUtils.getPingYin(academy.getAcademyName()));
			academy.setModelStatus(ModelStatus.add);
		} else {
			academy.setModelStatus(ModelStatus.update);
		}
		
		ActionResult actionResult;
		if (academyService.applyChange(academy)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		
		model.addAttribute("academy", academy);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (academyService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
	
	@RequestMapping(value = getAcademyBySchoolIdViewName, method = {RequestMethod.POST,RequestMethod.GET },produces="text/plain;charset=utf-8")
	public @ResponseBody String getAcademyBySchoolId(String schoolId){
		List<String> academyIds = academyService.queryListBySchoolId(schoolId);
		if(academyIds.size()<=0){
			return JsonUtils.writeValueAsString(null);
		}
		List<Academy> academys = academyService.queryList(academyIds);
		List<ListItem> items = academyListItem(academys);
		return JsonUtils.writeValueAsString(items);
	}
	
	private List<ListItem> academyListItem(List<Academy> academys){
		if(academys == null){
			return null;
		}
		List<ListItem> items = new ArrayList<ListItem>();
		ListItem item = null;
		for(Academy academy: academys){
			item = new ListItem();
			item.setText(academy.getAcademyName());
			item.setValue(academy.getAcademyId());
			items.add(item);
		}
		return items;
	}
}
